package basic.Test;

import java.io.Serializable;
import java.util.ArrayList;

public class CopyTestShallow {
	
	public static void main(String[]args) throws CloneNotSupportedException{
		
	    //teacher对象将被clone出来的Student对象共享.  
        Teacher teacher = new Teacher();  
        teacher.setAge(40);  
        teacher.setName("Teacher zhang");  
          
        Student student1 = new Student();  
        student1.setAge(20);  
        student1.setName("zhangsan");  
        student1.setTeacher(teacher);  
          
        //复制出来一个对象student2，但是此時student中的teacher引用指向同一個地方  
        Student student2 = (Student)student1.clone();  
        System.out.println(student2.getAge());  
        System.out.println(student2.getName());  
          
          
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
        System.out.println(student1.getTeacher().getAge());  
        System.out.println(student1.getTeacher().getName());  
          
          
        //修改student2的引用对象  
        student2.getTeacher().setAge(50);  
        student2.getTeacher().setName("Teacher Li");  
          
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
        System.out.println(student1.getTeacher().getAge());  
        System.out.println(student1.getTeacher().getName());  
        
        //集合类中是预定义类型，=，浅拷贝
        System.out.println();
        ArrayList list1=new ArrayList();
        list1.add("yangzhou");
        ArrayList list2=list1;
        list1.clear();

        if(list2.isEmpty()){
           System.out.println("浅拷贝");
        }
        
        //拷贝构造函数，预定义类型，深拷贝
        list1.add("yangzhou");
        ArrayList list3=new ArrayList(list1);
        list1.clear();

        if(list3.isEmpty()){
           System.out.println("浅拷贝");
        }else{
           System.out.println("深拷贝");
        }
        
        //在集合中，不管集合中的元素类型是预定义的还是自定义的，使用=就是浅拷贝，使用拷贝构造函数就是深拷贝
        list1.clear();
        list2.clear();
        list3.clear();
        list1.add(teacher);
        list2=list1;
        ArrayList list4=new ArrayList(list1);
        list1.clear();
        System.out.println(list2.toString());
        System.out.println(list4.toString());
        

        
	}

}
/*
20
zhangsan
~~~~~~~~~~~~~~~~~~~~~~
40
Teacher zhang
~~~~~~~~~~~~~~~~~~~~~~
50
Teacher Li
 * */

class Teacher implements Cloneable,Serializable{  
    public int age;  
    public String name;  
      
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    @Override  
    public Object clone() throws CloneNotSupportedException {  
        return super.clone();  
    }    
      
}  
  
class Student implements Cloneable{  
      
    public int age ;  
    public String name;  
    public Teacher teacher;  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public Teacher getTeacher() {  
        return teacher;  
    }  
    public void setTeacher(Teacher teacher) {  
        this.teacher = teacher;  
    }  
    @Override  
    public Object clone() throws CloneNotSupportedException {  
        return super.clone();  
    }  
      
}  